#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/7/11 11:00
# @Author  : CoderCharm
# @File    : role.py
# @Software: PyCharm
# @Desc    :
"""
角色表crud操作
"""

from typing import Optional
from sqlalchemy.orm import Session

from api.common.curd_base import CRUDBase
from api.models.auth import Role
from ..schemas import role_schema


class CRUDRole(CRUDBase[Role, role_schema.RoleCreate, role_schema.RoleUpdate]):

    @staticmethod
    def query_role(db: Session, *, id: int) -> Optional[Role]:
        """
        此role_id是否存在
        :param db:
        :param role_id:
        :return:
        """
        return db.query(Role).filter(Role.id == id).first()

    def create(self, db: Session, *, obj_in: role_schema.RoleCreate) -> Role:
        db_obj = Role(
            name=obj_in.name,
            pid=obj_in.pid,
        )
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)
        return db_obj


curd_role = CRUDRole(Role)
